/**
 * 物品栏类
 */
class ItemContainer extends egret.DisplayObjectContainer {
    /** 加载物品栏 */
    private item_container_png: egret.Bitmap = Main.createBitmapByName("item_container_png");
    /** 物品栏开关 */
    private item_container_btn: Boolean = false;
    /** 建立透明遮罩,触摸关闭物品栏 */
    private item_container_mask;
    /** 道具 */
    private item_png = new egret.Bitmap();
    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        this.itemContainerSetting();
        /** 道具栏的开关 */
        this.itemBtn();
    }
    /** 道具栏设置 */
    private itemContainerSetting() {
        this.addChild(this.item_container_png);
        this.item_container_png.x = 165;
        this.item_container_png.y = 176;

        this.item_container_png.touchEnabled = true;
        this.item_container_png.addEventListener(egret.TouchEvent.TOUCH_BEGIN, Main.superStopPropagation, this);
    }
    /** 道具栏的开关 */
    private itemBtn() {
        const btn = new egret.Shape();
        btn.graphics.beginFill(0, 0);
        btn.graphics.drawRect(0, 0, 200, 200);
        btn.graphics.endFill();
        btn.x = 218 - 50;
        btn.y = 185 - 100;
        this.addChild(btn);

        btn.touchEnabled = true;
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, btnHandler, this);
        /** 道具栏透明按钮点击处理器 */
        function btnHandler(evt) {
            this.item_container_btn = !this.item_container_btn;
            Main.superStopPropagation(evt);
            if (this.item_container_btn) {
                console.log("open item container")
                this.createMask();
                // this.setChildIndex(this.item_container_png, 90);
                // this.setChildIndex(btn, 95);
                try { this.setChildIndex(this.item_png, 99) } catch (err) { }
            } else {
                console.log("close item container")
                try { this.removeChild(this.item_container_mask) } catch (err) { }
            }
            this.dispatchEventWith("item_btn", false, this.item_container_btn, false);
        }
    }
    /** 建立透明遮罩,触摸关闭物品栏 */
    private createMask() {
        const mask = new egret.Shape();
        this.item_container_mask = mask;
        mask.graphics.beginFill(0xf45050, 0);
        mask.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        mask.graphics.endFill();
        this.addChild(mask);
        mask.touchEnabled = true;
        mask.addEventListener(egret.TouchEvent.TOUCH_BEGIN, maskHandler, this);
        /** 遮罩点击处理器 */
        function maskHandler(evt) {
            // Main.superStopPropagation(evt);
            this.removeChild(mask);
            this.item_container_btn = false;
            this.dispatchEventWith("item_btn", false, this.item_container_btn, false);
        }
    }
    /** 获取道具 */
    public getItem(item, tipPoint) {
        /** 创建背景光 */
        const linght = Main.createBitmapByName("item_get_png");
        this.addChild(linght);
        Main.anchorCenter(linght);
        linght.x = this.stage.stageWidth * .5;
        linght.y = this.stage.stageHeight * .5 - 415;
        /** 创建道具 */
        this.item_png = item;
        Main.anchorCenter(this.item_png);
        this.item_png.x = this.stage.stageWidth * .5;
        this.item_png.y = this.stage.stageHeight * .5 - 415;
        this.addChild(this.item_png);
        /** 道具获取动画 */
        egret.Tween.get(linght, { loop: true }).to({ rotation: 360 }, 10000);
        egret.Tween.get(item)
            .wait(1000)
            .call(() => this.removeChild(linght))
            .to({ x: this.item_container_png.x + this.item_container_png.width * .5, y: this.item_container_png.y + this.item_container_png.height * .5 }, 500);
        /** 道具拖动事件 */
        Main.dragGetPatn(this.item_png, this, {
            startFun: dragItemStart,
            moveFun: dragItemMove,
            endFun: dragItemEnd
        });
        /** 拖动道具开始处理器 */
        function dragItemStart(target, container) {
            container.startPath = { x: container.item_container_png.x + container.item_container_png.width * .5, y: container.item_container_png.y + container.item_container_png.height * .5 };
        }
        /** 拖动道具移动处理器 */
        function dragItemMove(target, container) {
            // console.log(container.startPath);
            if (target.hitTestPoint(tipPoint.x, tipPoint.y)) {
                console.log("碰到了");
                container.removeChild(container.item_png);
                // container.removeChild(container.mask);
                container.removeChild(container.item_container_mask);
                container.dispatchEvent(new egret.Event("hit"));
                container.item_container_btn = false;
                container.dispatchEventWith("item_btn", false, container.item_container_btn, false);
            }
        }
        /** 拖动道具结束处理器 */
        function dragItemEnd(target, container) {
            target.x = container.startPath.x;
            target.y = container.startPath.y;
        }
    }
}